const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderTab: '1',
    goodsType:'',
    servicesType:'',
    orderListGoods:[],
    orderListService:[],
    pageNum:1,
    userType:'',
    goodLoad:false,
    serviceLoad:false,
    goodsListEmp:false,
    ServiceListEmp:false
  },
  // 任务类型
  taskTypeBtn: function (e) {
    var { index } = e.currentTarget.dataset
    this.setData({
      orderTab: index
    })
    this.orderNum();
  },
  orderGood(e){
    this.setData({
      goodsType:  e.currentTarget.dataset.ordertype,
      pageNum:1,
      orderListGoods:[],
    })
    this.orderList(1,e.currentTarget.dataset.ordertype)
  },

  orderService(e){
    this.setData({
      servicesType:  e.currentTarget.dataset.ordertype,
      pageNum:1,
      orderListService:[],
    })
    this.orderList(2,e.currentTarget.dataset.ordertype)
  },


  // order类型
  orderBtn:function(e){
    var { ordertype } = e.currentTarget.dataset
    var { orderTab } = this.data
    wx.navigateTo({
      url: '/userPage/pages/notDealWith/notDealWith?ordertype=' + ordertype + '&orderTab=' + orderTab,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   /* this.setData({
      userType: wx.getStorageSync('userType')
    })
    this.orderList(2,'')
    this.orderList(1,'')*/
  },

  // 点击作品或服务列表
  shopList: function (e) {
    var { data,type } = e.currentTarget.dataset;
    if(data.status==='待付款'){
      if(type==='1'){
        console.log('作品')
        wx.navigateTo({
          url: '/taskPage/pages/worksPay/worksPay?title=' + data.title + '&cash=' + data.cash + '&orderid=' + data.id + '&code=' + data.code,
        })
      }else {
        console.log('服务')
        wx.navigateTo({
          url: '/taskPage/pages/hirePay/hirePay?price=' + data.cash + '&title=' + data.user_name + '&code=' + data.code + '&orderid=' + data.orderid + '&employid=' + data.id,
        })
      }

    }else {
      wx.navigateTo({
        url: '/taskPage/pages/worksDetail/worksDetail?id=' + data.object_id + '&type=1',
      })
    }
  },

  // 订单列表
  orderList:function(orderTab,ordertype){
    var { pageNum ,orderListGoods,orderListService,userType} = this.data
    var _this = this
    var url = ''
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      goodLoad:true,
      serviceLoad:true,
      goodsListEmp:false,
      ServiceListEmp:false,
    })
    var data = {
      type: orderTab,
      page: pageNum,
      token: app.globalData.token
    }
    if (orderTab == 1 && ordertype){
      data.goods_status = ordertype //作品
    }
    if(orderTab == 2 && ordertype){
      data.service_status = ordertype //服务
    }

    if (userType == 1){
      url = app.globalData.baseUrl + 'user/myBuyGoods'
    }else{
      url = app.globalData.baseUrl + 'user/mySaleGoods'
    }
    wx.request({
      url: url,
      data: data,
      success(res) {
        wx.hideLoading()
        _this.setData({
          goodLoad:false,
          serviceLoad:false
        })
        console.log(res)
        if (res.data.code == 1000) {
          if(res.data.data){
            if(orderTab == 1){
              _this.setData({
                orderListGoods: [...orderListGoods, ...res.data.data.data]
              })
            }else {
              _this.setData({
                orderListService: [...orderListService, ...res.data.data.data]
              })
            }
          }

          _this.setData({
            goodsListEmp:_this.data.orderListGoods.length===0,
            ServiceListEmp:_this.data.orderListService.length===0,
          })


        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },




  // 订单数量
  orderNum:function(){
    var { orderTab, userType } = this.data
    var _this = this
    var url = '';
    wx.showLoading({
      title: '加载中',
    })
    if (userType == 1){
      url = app.globalData.baseUrl + 'user/myBuyGoodsCount'
    }else{
      url = app.globalData.baseUrl + 'user/mySaleGoodsCount'
    }
    wx.request({
      url: url,
      data: {
        type: orderTab,
        token: app.globalData.token
      },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 1000) {
          _this.setData({
            orderNum: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('show------')
    this.setData({
      userType: wx.getStorageSync('userType'),
      orderListGoods:[],
      orderListService:[],
      pageNum:1,
    })
    this.orderList(2,'')
    this.orderList(1,'')
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var { pageNum,goodsType ,orderTab,servicesType} = this.data
    pageNum ++;
    this.setData({
      pageNum
    })
    if(orderTab==='1'){
      this.orderList(1,goodsType);
    }else {
      this.orderList(2,servicesType);
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
